package marcin.gui;

import java.awt.EventQueue;

import javax.swing.JFrame;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;
import com.jgoodies.forms.factories.FormFactory;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.JTextField;
import javax.swing.JCheckBox;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class AutoGUI {

	private JFrame frmAuto;
	private Auto autoDane;
	private JTextField txtMarka;
	private JTextField txtModel;
	private JTextField txtRokProdukcji;
	private JTextField txtPojemnosc;
	JCheckBox chckbxSprawne;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					AutoGUI window = new AutoGUI(new Auto());
					window.frmAuto.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public AutoGUI(Auto auto) {
		autoDane = auto;
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmAuto = new JFrame();
		frmAuto.setTitle("Auto");
		frmAuto.setBounds(100, 100, 450, 300);
		frmAuto.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frmAuto.getContentPane().setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),},
			new RowSpec[] {
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,}));
		
		JLabel lblMarka = new JLabel("Marka");
		lblMarka.setHorizontalAlignment(SwingConstants.RIGHT);
		frmAuto.getContentPane().add(lblMarka, "4, 4, right, default");
		
		txtMarka = new JTextField();
		frmAuto.getContentPane().add(txtMarka, "6, 4, fill, default");
		txtMarka.setColumns(10);
		
		JLabel lblModel = new JLabel("Model");
		lblModel.setHorizontalAlignment(SwingConstants.RIGHT);
		frmAuto.getContentPane().add(lblModel, "4, 8, right, default");
		
		txtModel = new JTextField();
		frmAuto.getContentPane().add(txtModel, "6, 8, fill, default");
		txtModel.setColumns(10);
		
		JLabel lblRokProdukcji = new JLabel("Rok produkcji");
		lblRokProdukcji.setHorizontalAlignment(SwingConstants.RIGHT);
		frmAuto.getContentPane().add(lblRokProdukcji, "4, 12, right, default");
		
		txtRokProdukcji = new JTextField();
		frmAuto.getContentPane().add(txtRokProdukcji, "6, 12, fill, default");
		txtRokProdukcji.setColumns(10);
		
		JLabel lblPojemnosc = new JLabel("Pojemno\u015B\u0107");
		lblPojemnosc.setHorizontalAlignment(SwingConstants.RIGHT);
		frmAuto.getContentPane().add(lblPojemnosc, "4, 16, right, default");
		
		txtPojemnosc = new JTextField();
		frmAuto.getContentPane().add(txtPojemnosc, "6, 16, fill, default");
		txtPojemnosc.setColumns(10);
		
		chckbxSprawne = new JCheckBox("sprawne");
		frmAuto.getContentPane().add(chckbxSprawne, "4, 20");
		
		JButton btnZapisz = new JButton("Zapisz");
		btnZapisz.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				zapiszDane();
			}
		});
		frmAuto.getContentPane().add(btnZapisz, "6, 20");

		txtMarka.setText( autoDane.getMarka() );
		txtModel.setText( autoDane.getModel() );
		txtRokProdukcji.setText( Integer.toString( autoDane.getRokProdukcji() ) );
		txtPojemnosc.setText( Double.toString( autoDane.getPojemnosc() ) );
		chckbxSprawne.setSelected( autoDane.isSprawne() );
		
	}
	
	public void zapiszDane(){
		autoDane.setMarka(txtMarka.getText());
		autoDane.setModel(txtModel.getText());
		autoDane.setRokProdukcji(Integer.parseInt(txtRokProdukcji.getText()));
		autoDane.setPojemnosc(Double.parseDouble(txtPojemnosc.getText()));
		autoDane.setSprawne(chckbxSprawne.isSelected());
		autoDane.wypisz();
	}
}
